<?php
namespace Index\Controller;
use Think\Controller;
class SearchController extends CommonController {
    public function indexAction(){
    	$q = I('get.q','');
    	if(!empty($q)){
    		$arr = FC($q);

    		if(empty($arr)){
    			$arr[]=$q;
    		}

    		$sql = "select * from question where";

    		foreach ($arr as $key => $v) {
    			if($key==(count($arr)-1)){
    				$sql .= " title like '%{$v}%'";
    			}else{
    				$sql .= " title like '%{$v}%' or";
    			}
    		}

    		$res = M()->query($sql);


    		foreach ($res as &$value) {
    			foreach ($arr as $v) {
    				$value['title'] = str_ireplace($v, "<span style='color:red'>".$v."</span>", $value['title']);
    			}
    		}

    		// var_dump($res);exit;

    		$this->assign('res',$res);
    		$this->display();

    		
    	}else{
    		$this->error('非法请求',U('index/index/index'));
    	}
        // 
    }
} 